require "rubygems"
require "active_support"

class DateInputParser
  WEEK = "w"
  MONTH="m"
  DAY="d"
  
  def getAbsoluteTime(time)
    dateToBeChecked = getDateToBeChecked(time)
    absoluteTime = dateToBeChecked.year.to_s+"-"+dateToBeChecked.month.to_s+"-"+dateToBeChecked.day.to_s
    absoluteTime
  end
  
  def getDateToBeChecked(time)
    timeLowerCase = time.downcase
    if timeLowerCase.include?MONTH
      dateToBeChecked = getDateToBeCheckedByMonth(time)
    elsif timeLowerCase.include?WEEK
      dateToBeChecked = getDateToBeCheckedByWeek(time)
    elsif timeLowerCase.include?DAY
      dateToBeChecked = getDateToBeCheckedByDay(time)
    else
      raise ArgumentError, "Invalid time input!", caller
    end
    dateToBeChecked
  end
  
  def getDateToBeCheckedByMonth(months)
    numberOfMonths = months.chomp(MONTH)
    return Time.now - numberOfMonths.to_i.months
  end
  
  def getDateToBeCheckedByWeek(weeks)
    numberOfWeeks = weeks.chomp(WEEK)
    return Time.now - numberOfWeeks.to_i.weeks
  end
  
  def getDateToBeCheckedByDay(days)
    numberOfDays = days.chomp(DAY)
    return Time.now - numberOfDays.to_i.days
  end
  
  private :getDateToBeCheckedByMonth, :getDateToBeCheckedByWeek, :getDateToBeCheckedByDay
end